package com.gtq.uhome.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @description:
 * @author: Zhong Ranjie
 * @create: 2020-07-31 09:21
 **/
@ApiModel("通用返回类")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "返回标记：成功标记=0000，失败标记：对应系统业务编码")
    private String code;
    private String token;
    @ApiModelProperty(value = "返回信息")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;

    public static <T> Result<T> ok() {
        return (Result<T>) restResult(null, "0000", null);
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, "0000", null);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return restResult(data, "0000", msg);
    }

    public static <T> Result<T> failed() {
        return (Result<T>) restResult(null, "0001", null);
    }

    public static <T> Result<T> failed(String msg) {
        return (Result<T>) restResult(null, "0001", msg);
    }

    public static <T> Result<T> failed(T data) {
        return restResult(data, "0001", null);
    }

    public static <T> Result<T> failed(T data, String msg) {
        return restResult(data, "0001", msg);
    }

    public static <T> Result<T> restResult(T data, String code, String msg) {
        Result<T> apiResult = new Result();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public boolean isSuccess() {
        return "0000".equals(this.code) || "0".equals(this.code);
    }

    @Override
    public String toString() {
        return "Result(code=" + this.getCode() + ", token=" + this.getToken() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    public Result() {
    }

    public Result(String code, String token, String msg, T data) {
        this.code = code;
        this.token = token;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return this.code;
    }

    public Result<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public String getToken() {
        return this.token;
    }

    public Result<T> setToken(String token) {
        this.token = token;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
}
